package com.admin.service.impl;

import com.admin.entity.SysContentEntity;
import com.admin.mapper.SysContentMapper;
import com.admin.service.SysContentService;
import com.admin.utils.ConvertUtils;
import com.admin.web.PageResp;
import com.admin.web.request.ContentAddReq;
import com.admin.web.request.ContentPageReq;
import com.admin.web.request.ContentUpdateReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * 内容服务
 */
@Service("sysContentService")
public class SysContentServiceImpl implements SysContentService {

    @Resource
    private SysContentMapper sysContentMapper;

    @Override
    public void save(ContentAddReq contentAddReq) {
        SysContentEntity sysContentEntity = ConvertUtils.convert(contentAddReq, SysContentEntity.class);
        sysContentMapper.insert(sysContentEntity);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        sysContentMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(ContentUpdateReq contentUpdateReq) {
        SysContentEntity sysContentEntity = ConvertUtils.convert(contentUpdateReq, SysContentEntity.class);
        sysContentMapper.updateById(sysContentEntity);
    }

    @Override
    public PageResp<SysContentEntity> page(ContentPageReq contentPageReq) {
        LambdaQueryWrapper<SysContentEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if (!StringUtils.isEmpty(contentPageReq.getTitle())) {
            queryWrapper.like(SysContentEntity::getTitle, contentPageReq.getTitle());
        }
        IPage<SysContentEntity> iPage = sysContentMapper.selectPage(new Page<>(contentPageReq.getPage(), contentPageReq.getLimit()), queryWrapper);
        return ConvertUtils.convertPageResp(iPage);
    }


}